package com.example.service;

import com.example.bean.Venue;
import com.example.bean.VenueBooking;
import com.example.bean.VenueOrder;
import com.example.mapper.VenueMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class VenueService {

    @Resource
    VenueMapper venueMapper;

    //查询全部场地
//    public PageInfo<Venue> queryAllVenue(Integer pageNum,Integer pageSize) {
//        PageHelper.startPage(pageNum,pageSize);
//        List<Venue> venues = venueMapper.queryAllVenue();
//        PageInfo<Venue> pageInfo = new PageInfo<>(venues);
//        return pageInfo;
//    }

    public List<Venue> queryAllVenue() {
        return venueMapper.queryAllVenue();
    }

    //通过ID查询场地
    public Venue queryVenueById(int venueId) {
        return venueMapper.queryVenueById(venueId);
    }

    //通过名字模糊查询场地
//    public PageInfo<Venue> queryVenueByName(Integer pageNum,Integer pageSize,String venueName) {
//        PageHelper.startPage(pageNum,pageSize);
//        List<Venue> venues = venueMapper.queryVenueByName(venueName);
//        PageInfo<Venue>pageInfo = new PageInfo<>(venues);
//        return pageInfo;
//    }

    public List<Venue> queryVenueByName(String venueName) {
        venueName = "%" + venueName + "%";
        return venueMapper.queryVenueByName(venueName);
    }

    public List<Venue> queryVenueByUserId(int user) {
        return venueMapper.queryVenueByUserId(user);
    }

    //添加场地
    public int addVenue(Venue venue) {
        return venueMapper.addVenue(venue);
    }

    //删除场地
    public boolean deleteVenue(int venueId) {
        Venue venue = venueMapper.queryVenueById(venueId);
        if (venue.getState() != 0) return false;
        venueMapper.deleteVenue(venueId);
        return true;
    }

    //修改场地
    public int updateVenue(Venue venue) {
        return venueMapper.updateVenue(venue);
    }

    //更改场地状态
    public int updateVenueState(int venueId, int state) {
        return venueMapper.updateVenueState(venueId, state);
    }

    //查询场地费用
    public String venueCharges(int venueId) {
        return venueMapper.venueCharges(venueId);
    }


    public String announcement() {
        return null;
    }

    //查询场地一周信息
    public String weeklyInfo(int venueId) {
        return venueMapper.weeklyInfo(venueId);
    }
//
//    //保存订单信息
//    public int saveVenueOrder(String startTime, String endTime, int venueId) throws ParseException {
//        //如果场地状态为1，直接返回0，告知场地已被预定
//        if(venueMapper.queryVenueById(venueId).getState() == 1){
//            return 0;
//        }else {
//            //订单状态,订单时间大于60为已完成（1），小于60为失约（0）
//            int state;
//            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm");
//            Date start = simple.parse(startTime);
//            Date end = simple.parse(endTime);
//            long from = start.getTime();
//            long to = end.getTime();
//
//            //计算订单总时间，单位为分钟
//            int countTime = (int) ((to-from)/(1000*60));
//            int charges = venueMapper.queryVenueById(venueId).getCharges();//获取场地单价
//
//            //计算订单总费用
//            int countCharges = countTime*charges;
//
//            if(countTime<60){
//                state = 0;
//            }else state = 1;
//
//            VenueOrder venueOrder = new VenueOrder(1,start,end,countTime,charges,countCharges,venueId,venueOrderId,state);
//            return venueMapper.saveVenueOrder(venueOrder);
//        }
//    };

    //预定场地
    public int venueBooking(VenueBooking venueBooking) {
        return venueMapper.saveVenueBooking(venueBooking);
    }

    //取消预定
    public int cancelBook(int userId) {
        return venueMapper.cancelBook(userId);
    }

    //查询预定信息
    public List<VenueBooking> queryVenueBooking() {
        return venueMapper.queryVenueBooking();
    }

    public int newBooking(VenueOrder venueOrder) {
        venueOrder.setStartTime(new Date());
        return venueMapper.newVenueBooking(venueOrder);
    }

    public int endBooking(int venueOrderId) {
        VenueOrder venueOrder = venueMapper.selectByOrderId(venueOrderId);
        int venueId = venueOrder.getVenueId();
        Venue venue = venueMapper.queryVenueById(venueId);
        venue.setUser(0);
        venue.setState(0);
        venueMapper.updateVenue(venue);
        Date endTime = new Date();
        Date startTime = venueOrder.getStartTime();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        long from = startTime.getTime();
        long to = endTime.getTime();

        //计算订单总时间，单位为分钟
        int countTime = (int) ((to - from) / (1000 * 60));


        //计算订单总费用
        int countCharges = countTime * venueOrder.getCharges();
        venueOrder.setEndTime(endTime);
        venueOrder.setCountCharges(countCharges);
        venueOrder.setCountTime(countTime);
        venueOrder.setOrderState(1);
        return venueMapper.updateVenueOrder(venueOrder);
    }


    public List<VenueOrder> queryVenueOrderByUserId(int id) {
        return venueMapper.queryVenueOrderByUserId(id);
    }


    public List<VenueOrder> queryAllVenueOrder() {
        return venueMapper.queryAllVenueOrder();
    }
}
